<?php
/**
 * Display list of last changes
 *
 */

use Hive\Ressource\Document;
use Hive\Ressource\Wish;
use Hive\Ressource\Interview;
use Hive\Ressource\Experience;
use Hive\Ressource;
use Doctrine\DBAL\Connection;
use Fox\Dao\SqlFragment;
use Fox\Version;

$xview = Xview::instance();
$user = User::current();
$idusr = $user->getId();
$limit = $xview->val('limit',7);
$excluded = array('documents','address');
$data = array();
// First Count the total number of events
$query = \Fox\Dao::infos(Version::className())->handle()->createQueryBuilder();
$query->select('COUNT(*)')
	  ->from('versions','v')
	  ->groupBy(array('handlerkey','handlerid','changetype','author'));

$fragment = new SqlFragment('handlerkey NOT IN (?) ',array($excluded),array(Connection::PARAM_STR_ARRAY));

$nbEvents = $fragment->pushIn($query)->execute()->fetchColumn();

if($xview->isPrintMode()) {
	$limit = 200;
} else {
	$limit = 400;
}
// Select by group of 200
$query = \Fox\Dao::infos(Version::className())->handle()->createQueryBuilder();
$query->select('DISTINCT handlerkey','handlerid','changetype','author','date')
	->from('versions','ve')
	->groupBy(array('handlerkey','handlerid','changetype','author','date'))
	->setMaxResults($limit)
	->orderBy('date','DESC');

$fragment = new SqlFragment('handlerkey NOT IN (?) ',array($excluded),array(Connection::PARAM_STR_ARRAY));
$result = $fragment->pushIn($query)->execute()->fetchAll(PDO::FETCH_ASSOC);

if(is_array($result))
{
	foreach($result as $version)
	{
		$date = substr($version['date'],0,16);

		$id = $version['handlerkey'].'_'.$version['handlerid'];
		$uniqueId = $id.'_'.$date;
		switch($version['handlerkey'])
		{

			case 'ressources' :
				try {

					$ressources[$version['handlerid']] = Fox\Dao::load(Ressource::className(), $version['handlerid']);
					$name = $ressources[$version['handlerid']]->name();
					$ressourceIds[] = $version['handlerid'];
					switch($version['changetype']) {
						case 'create':
							// Document has been Uploaded
							$message = Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_CREATE,'<b>'.$name.'</b>');
							break;
						case Ressource::CHANGE_DOCUMENTS:
							// Document has been Uploaded
							$message = Msg(Hive::BUNDLE,Hive::MSG_NEW_RESUME_FOR,$name);
							break;
						case Ressource::CHANGE_DOCUMENT_DELETE:
							// A Resume has been deleted
							$message = Msg(Hive::BUNDLE,Hive::MSG_RESUME_REMOVE_FROM,$name);
							break;
						default:
							$message = Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_UPDATE,'<b>'.$name.'</b>');
							break;

					}
					$line['event'] = $message;
					$line['author'] = $version['author'];
					$line['date'] = $version['date'];
					$uniqueId = $date.'_'.$line['event'];
				}
				catch (Fox\Object\Exception\NotExist $e)
				{

				}

				break;
			case 'ressource_resume' :
			case 'ressource_documents' :
			case 'documents' :
				try {
					$resume = Fox\Dao::load(Document::className(), $version['handlerid']);
					$ressourceIds[] = $resume->ressource_id;
				}
				catch (Fox\Object\Exception\NotExist $e)
				{

				}

				break;
			case 'companies' :
				break;
			case 'ressource_experience' :
				try {
					$object = Fox\Dao::load(Experience::className(), $version['handlerid']);
					$ressourceIds[] = $object->ressource_id;
				}
				catch (Fox\Object\Exception\NotExist $e)
				{

				}
				break;
			case 'ressource_interviews' :
				try {
					$object = Fox\Dao::load(Interview::className(), $version['handlerid']);
					$ressourceIds[] = $object->ressource_id;
				}
				catch (Fox\Object\Exception\NotExist $e)
				{

				}
				break;
			case 'ressource_desires' :
				try {
					$object = Fox\Dao::load(Wish::className(), $version['handlerid']);
					$ressourceIds[] = $object->ressource_id;
				}
				catch (Fox\Object\Exception\NotExist $e)
				{

				}
				break;
			case 'schools' :
				break;
			case 'missions' :
				break;
			case 'mission_announces' :
				break;
		}
		if(!empty($line))
			$data[$uniqueId] = $line;
		if(count($data) > $limit)
			break;
	}
	// Delete
}

$grid = new Fox\Grid('gEvents');
$grid->fieldcat(array('event','date','author'));
$grid->fieldcat()->field('date')->DisplayTime();
$grid->fieldcat()->field('author')
					->align('center')
					->width(15)
					->SetCexit('\User::fullName');
$grid->initialize($data);
$grid->layout()->scrollVertical(true);
if($xview->isPrintMode() || $xview->ajax()) {
	return $grid->display();
}
else
{
	$screen = new Fox\Html\Screen("sActivities",null);
	$screen->pageTitle(Msg(Hive::BUNDLE,Hive::LABEL_EVENTS));
	$screen->add($grid);
	echo $screen->display();
}